from GlobalObj import *
import logging
import time

from RiChang import *


logger = logging.getLogger(__name__)

class DailyBase:
    RCName = 'DailyBase'
    LogHeader = RCName
    started = False
    enum = None
    feiZhanDouShu = 0
    yiChangShu = 0
    areaDict = {}
    status = -1
    waitTime = 2.0
    preStatus = -1
    def __init__(self):
        pass

    def loadPrivateActions(self, areaDict, actions):

        loadActions(self.RCName, areaDict, actions)
        logger.info("{} has {} actions".format(self.LogHeader, len(areaDict)))

    def print(self):
        logger.info(self.LogHeader)

    def begin(self, data=None, try_times=8, isDaily=True):

        self.feiZhanDouShu = 0
        self.yiChangShu = 0
        self.status = -1
        self.waitTime = 2.0

        self.started = True
        logger.info("isDaily {}".format(isDaily))

        if isDaily is True:

            if self.GoFeiZhanDou(try_times) is STATUS_NOTFOUND:
                return STATUS_NOTFOUND

            if self.GoRiChang() is STATUS_NOTFOUND:
                return STATUS_NOTFOUND_DAILY

        while(self.started):
            logger.info(self.LogHeader + "!!!!!!!!")
            img = self.enum.screenCap()
            if img is None:
                continue

            self.preStatus = self.status

            status, self.feiZhanDouShu, self.yiChangShu = gPublic.detectZhanDou(img, self.feiZhanDouShu, self.yiChangShu)

            if status != STATUS_OTHERS:
                self.preStatus = self.status

            self.status = status

            logger.info("{} feizhandou: {} yichang: {}".format(self.LogHeader, self.feiZhanDouShu, self.yiChangShu))

            self.process_private(img, data)

            time.sleep(self.waitTime)


    def GoRiChang(self, try_times = 8):

        self.enum.click(clickPos = InRiChang, delay = 800)

        #self.enum.swipe(())

        while(self.started and try_times > 0):
            try_times -= 1
            logger.info(self.LogHeader + "!!!!!!!! " + str(try_times))
            img = self.enum.screenCap()
            if img is None:
                continue

            ret = gRiChang.CheckRiChang(self.RCName, self.enum, img)
            if ret == STATUS_NOTFOUND:
                self.enum.swipe(RiChangSwipe, delay=800)
            elif ret == STATUS_FINISHED:
                return STATUS_NOTFOUND
            else:
                return STATUS_OK

        logger.info("{} not found ".format(self.RCName))
        self.enum.click(closeRiChang,500)
        return STATUS_NOTFOUND



    def GoFeiZhanDou(self, try_times = 8):

        while(self.started and try_times > 0):
            try_times -= 1
            logger.info(self.LogHeader + "!!!!!!!!")
            img = self.enum.screenCap()
            if img is None:
                continue

            #gPublic.closeWindow(self.enum, img)

            self.status, self.feiZhanDouShu, self.yiChangShu = gPublic.detectZhanDou(img, self.feiZhanDouShu, self.yiChangShu)
            if self.status == STATUS_OTHERS:
                gPublic.closeWindow(self.enum, img)
            elif self.status == STATUS_FEIZHANDOU:
                return STATUS_OK
            else:
                try_times += 1
                time.sleep(5)

        return STATUS_NOTFOUND




    def stop(self):
        logger.info("{} stoped".format(self.RCName))
        self.started = False

    def process_private(self, img = None, data = None):
        logger.info("base")
